try:
    import cython
except:
    raise ImportError('Sorry the required package cython was not found')

try:
    import CoolProp
except ImportError:
    raise ImportError('The required python package CoolProp was not found.  Please go to coolprop.sf.net to obtain a copy')
    
from distutils.core import setup, Extension
from Cython.Build import cythonize
from Cython.Distutils import build_ext
from Cython.Distutils.extension import Extension as CyExtension
import sys, shutil, os, glob

version = '0.0'

## #Modify the __init__ file with this version string
## fName = os.path.join('PDSim','__init__.py')
## lines = open(fName,'r').readlines()
## fp = open(fName,'w')
## for line in lines:
##     if line.startswith('__version__'):
##         line = "__version__ = '" + version + "'"
##     fp.write(line+'\n')
## fp.close()

if len(sys.argv) == 1:
    #sys.argv+=['build_ext','--force','--inplace']
    #sys.argv+=['build_ext','--inplace']
    #sys.argv+=['build','--force','install']
    sys.argv+=['build','install']

import Cython

#This will generate HTML to show where there are still pythonic bits hiding out
Cython.Compiler.Options.annotate = True

#Get the numpy include folder
import numpy

#Each of the Pure-Python or PYX files in this list will be compiled to a python module       
pyx_list = [
            "ThermalCorr/TwoPhaseHTDP.pyx",
            ]

def clean():
    for pyx_file in pyx_list:
        f_root = pyx_file.rsplit('.',1)[0]
        for ending in ['.pyd','.so']:
            fname = f_root+ending
            try:
                os.remove(fname)
                print 'removed',fname
            except OSError:
                pass
            
# Try to remove the generated files in the source tree 
# if you are doing an install to the normal location
if '--inplace' not in sys.argv or '--clean' in sys.argv:
    clean()
    if '--clean' in sys.argv:
        sys.argv.remove('--clean')

pxd_files = []

others = glob.glob(os.path.join('..','..','src','CoolProp','CoolProp','*.cpp'))
cpp_files = glob.glob(os.path.join('..','..','src','*.cpp'))
    
ext_module_list = []
for pyx_file in pyx_list:
    sources = [pyx_file]
    #Try to find a PXD backpack if possible
    pxd_file = pyx_file.rsplit('.',1)[0]+'.pxd'
    if os.path.exists(pxd_file):
        sources+=[pxd_file]
    pxd_files.append(pxd_file)
    
    #Build an extension with the sources
    ext_name = pyx_file.rsplit('.',1)[0].replace('/','.')

    ext_module_list.append(CyExtension(ext_name,
                                        sources + cpp_files + others,
                                        language = 'c++',
                                        include_dirs = [os.path.join('..','..','src','CoolProp','CoolProp'),
                                                        os.path.join('..','..','src')]))
       

setup(
  name = 'ThermalCorr',
  version = version,
  author = "Ian Bell",
  author_email='ian.h.bell@gmail.com',
  url='http://pdsim.sourceforge.net',
  description = """ Open-source heat transfer and pressure drop correlations """,
  packages = ['ThermalCorr'],
  cmdclass={'build_ext': build_ext},
  ext_modules = ext_module_list,
  #package_data = {'ThermalCorr':pxd_files,'PDSim.include':glob.glob(os.path.join('CoolProp','*.h'))},
  #include_dirs = [CoolProp.get_include_directory()]
)

import ThermalCorr
print dir(ThermalCorr)
